package com.khshop.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.khshop.mapper.ProductMapper;
import com.khshop.pojo.Product;
import com.khshop.service.ProductService;
import com.khshop.vo.AjaxResult;
import com.khshop.vo.ServiceResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.util.Calendar;
import java.util.UUID;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author zhaoyang
 * @since 2020-10-14
 */
@Api(tags = "商品 前端控制器")
@Controller
@RequestMapping("/")
public class ProductController {

    @Autowired(required = false)
    ProductService productService;

    @ApiOperation("新增商品数据")
    @PostMapping("/addProduct")
    @ResponseBody
    public AjaxResult addProduct(@Valid Product product,@RequestPart(value = "photo",required = false) MultipartFile multipartFile){
        System.err.println("新增的商品信息:"+product+"上传文件信息："+multipartFile);
        if (!multipartFile.isEmpty()) {
            //获得SpringBoot当前项目的路径：System.getProperty("user.dir")
            String path = System.getProperty("user.dir")+"/images/";
            //按照月份进行分类：
            Calendar instance = Calendar.getInstance();
            String month = (instance.get(Calendar.MONTH) + 1)+"Month";
            path = path+month;

            System.err.println("获得的文件夹路径:" + path);
            String fName = multipartFile.getOriginalFilename();
            String houZhui = fName.substring(fName.lastIndexOf("."));
            String fileName = UUID.randomUUID() + houZhui;
            File file = new File(path, fileName);
            System.err.println("路径：" + path + ",名称：" + fileName);
            if (!file.exists()) {
                file.mkdirs();     //判断这个文件是否存在
            }
            product.setImgs(path+"/"+fileName);
            try {
                multipartFile.transferTo(file);// 将上传文件保存到相应位置
            } catch (Exception e) {
                e.printStackTrace();

            }

        }
        if (productService.addProduct(product).isFlag()) {
            return  AjaxResult.success("增加成功");
        } else {
            return  AjaxResult.error("增加失败，请稍后重试！");
        }
    }

    @ApiOperation("修改库存")
    @PostMapping("/addProductStock")
    @ResponseBody
    public AjaxResult addProductStock(Long id,int num){
        if (productService.addProductStock(id,num).isFlag()) {
            return  AjaxResult.success("修改成功");
        } else {
            return  AjaxResult.error("修改失败，请稍后重试！");
        }
    }

    @ApiOperation("修改商品数据")
    @PostMapping("/updateProduct")
    @ResponseBody
    public AjaxResult updateProduct(Product product){
        if (productService.updateProduct(product).isFlag()) {
            return  AjaxResult.success("更新成功");
        } else {
            return  AjaxResult.error("更新失败，请稍后重试！");
        }
    }

    @ApiOperation("逻辑删除商品数据")
    @PostMapping("/delProduct")
    @ResponseBody
    public AjaxResult delProduct(Long id){
        if (productService.delProduct(id).isFlag()) {
            return  AjaxResult.success("删除成功");
        } else {
            return  AjaxResult.error("删除失败，请稍后重试！");
        }
    }

    @ApiOperation("模糊分页查询")
    @PostMapping("/searchProduct")
    @ResponseBody
    public AjaxResult searchProduct(@RequestParam("name")String name, @RequestParam("status")boolean status, @RequestParam("salenums")Integer salenums, @RequestParam("price")Float price, @RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "limit",defaultValue = "15") Integer limit){
        QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
        ServiceResult<IPage<Product>> result = productService.searchProduct(queryWrapper,page,limit);
        if (result.isFlag()) {
            return AjaxResult.success((int) result.getData().getTotal(), result.getData());
        } else {
            return AjaxResult.error("网络超时，请稍后重试！");
        }
    }

    @ApiOperation("批量修改商品上架，下架状态")
    @PostMapping("/onsaleProduct")
    @ResponseBody
    public AjaxResult onsaleProduct(Long []id,boolean status){
        if (productService.onsaleProduct(id,status).isFlag()) {
            return  AjaxResult.success("修改成功");
        } else {
            return  AjaxResult.error("修改失败，请稍后重试！");
        }
    }

    @ApiOperation("条件排序")
    @PostMapping("/productSort")
    @ResponseBody
    public AjaxResult productSort(String name, boolean status, Integer salenums, Float price, @RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "limit",defaultValue = "1") Integer limit){
        return null;
    }
}

